<template>
  <a-dropdown v-if="currentUser && currentUser.name" placement="bottomRight">
    <span class="ant-pro-account-avatar" v-if="userInfo && userInfo.username">
      <!-- <a-avatar
        size="small"
        :src="userInfo.avatar | avatarfilter"
        class="antd-pro-global-header-index-avatar"
        v-if="userInfo && userInfo.avatar"
      /> -->
      <a-avatar
        size="small"
        :src=" avatar | avatarfilter"
        class="antd-pro-global-header-index-avatar"
        v-if="userInfo && userInfo.avatar"
      />
      <a-avatar size="small" v-else :src="defaultimg" class="antd-pro-global-header-index-avatar" />
      <span>{{ userInfo.username }}</span>
    </span>
    <span class="ant-pro-account-avatar" v-else>
      <a-avatar
        size="small"
        src="https://gw.alipayobjects.com/zos/antfincdn/XAosXuNZyF/BiazfanxmamNRoxxVxka.png"
        class="antd-pro-global-header-index-avatar"
      />
      <span>{{ currentUser.name }}</span>
    </span>
    <template v-slot:overlay>
      <a-menu class="ant-pro-drop-down menu" :selected-keys="[]">
        <!-- <a-menu-item v-if="menu" key="center" @click="handleToCenter">
          <a-icon type="user" />
          {{ $t('menu.account.center') }}
        </a-menu-item> -->
        <a-menu-item v-if="menu" key="settings" @click="handleToSettings">
          <a-icon type="setting" />
          {{ $t('menu.account.settings') }}
        </a-menu-item>
        <a-menu-divider v-if="menu" />
        <a-menu-item key="logout" @click="handleLogout">
          <a-icon type="logout" />
          {{ $t('menu.account.logout') }}
        </a-menu-item>
      </a-menu>
    </template>
  </a-dropdown>
  <span v-else>
    <a-spin size="small" :style="{ marginLeft: 8, marginRight: 8 }" />
  </span>
</template>

<script>
// import user from '@/locales/lang/zh-CN/user'
import { Modal } from 'ant-design-vue'
// import { map } from 'core-js/core/array'
import { mapGetters } from 'vuex'
export default {
  name: 'AvatarDropdown',
  props: {
    currentUser: {
      type: Object,
      default: () => null
    },
    menu: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      defaultimg: require('@/assets/defaultimg.jpg')
    }
  },
  computed: {
    ...mapGetters(['userInfo', 'avatar'])
  },
  filters: {
    avatarfilter(e) {
      return JSON.parse(e).url
    }
  },
  mounted() {
    // console.log('用户信息1', this.avatar)
  },
  methods: {
    handleToCenter() {
      this.$router.push({ path: '/userInfo/securitySettings' })
    },
    handleToSettings() {
      this.$router.push({ path: '/userInfo/basic' })
    },
    handleLogout(e) {
      Modal.confirm({
        title: this.$t('layouts.usermenu.dialog.title'),
        content: this.$t('layouts.usermenu.dialog.content'),
        onOk: () => {
          // return new Promise((resolve, reject) => {
          //   setTimeout(Math.random() > 0.5 ? resolve : reject, 1500)
          // }).catch(() => console.log('Oops errors!'))
          return this.$store.dispatch('Logout').then(() => {
            this.$router.push({ name: 'login' })
          })
        },
        onCancel() {}
      })
    }
  }
}
</script>

<style lang="less" scoped>
.ant-pro-drop-down {
  :deep(.action) {
    margin-right: 8px;
  }
  :deep(.ant-dropdown-menu-item) {
    min-width: 160px;
  }
}
</style>
